all: patch1 patch2 patch3 patch4
	@becho "All patches applied!"

patch1: ../nspireio-2.0
	@becho "Applying patch '01-compu-svntrunk-r35-makefile_userprofile_var_linux_fix.patch'..."
	cd ../nspireio-2.0/ && patch -Ns -p0 < ../patches/01-compu-svntrunk-r35-makefile_userprofile_var_linux_fix.patch && cd ../patches && touch patch1

patch2: ../nspireio-2.0
	@becho "Applying patch '02-compu-svntrunk-r36-draw_mode_console_clear_fix.patch'..."
	cd ../nspireio-2.0/ && patch -Ns -p0 < ../patches/02-compu-svntrunk-r36-draw_mode_console_clear_fix.patch && cd ../patches && touch patch2

patch3: ../nspireio-2.0
	@becho "Converting source file(s) CRLF to LF to prepare for patch 3..."
	cd ../nspireio-2.0 && for FILE in LICENSE src/charmap.h src/console.c src/demo/ConsoleDemo.c src/nspireio2.h src/registry.c src/screen.c src/uart.c; do \
	  echo " = Converting $$FILE..."; \
	  cat $$FILE | tr -d '\r' > $$FILE.LF && mv $$FILE.LF $$FILE; \
	done

	@becho "Applying patch '03-compu-svntrunk-r37-lgpl_license_change.patch'..."
	cd ../nspireio-2.0/ && patch -Ns -p0 < ../patches/03-compu-svntrunk-r37-lgpl_license_change.patch && cd ../patches && touch patch3

patch4: ../nspireio-2.0
	@becho "Converting source file(s) CRLF to LF to prepare for patch 4..."
	cd ../nspireio-2.0 && for FILE in src/Makefile; do \
	  echo " = Converting $$FILE..."; \
	  cat $$FILE | tr -d '\r' > $$FILE.LF && mv $$FILE.LF $$FILE; \
	done

	@becho "Applying patch '04-alberth-local-standardize_makefile.patch'..."
	cd ../nspireio-2.0/ && patch -Ns -p0 < ../patches/04-alberth-local-standardize_makefile.patch && cd ../patches && touch patch4

clean:

rmpatch1: patch1
	@becho "Reversing patch '01-compu-svntrunk-r35-makefile_userprofile_var_linux_fix.patch'..."
	cd ../nspireio-2.0/ && patch -Rs -p0 < ../patches/01-compu-svntrunk-r35-makefile_userprofile_var_linux_fix.patch && cd ../patches && rm patch1

rmpatch2: patch2
	@becho "Reversing patch '02-compu-svntrunk-r36-draw_mode_console_clear_fix.patch'..."
	cd ../nspireio-2.0/ && patch -Rs -p0 < ../patches/02-compu-svntrunk-r36-draw_mode_console_clear_fix.patch && cd ../patches && rm patch2

rmpatch3: patch3
	@becho "Reversing patch '03-compu-svntrunk-r37-lgpl_license_change.patch'..."
	cd ../nspireio-2.0/ && patch -Rs -p0 < ../patches/03-compu-svntrunk-r37-lgpl_license_change.patch && cd ../patches && rm patch3

rmpatch4: patch4
	@becho "Reversing patch '04-alberth-local-standardize_makefile.patch'..."
	cd ../nspireio-2.0/ && patch -Rs -p0 < ../patches/04-alberth-local-standardize_makefile.patch && cd ../patches && rm patch4

../nspireio-2.0:
	make -C .. nspireio-2.0

distclean-init:
	@becho "Cleaning up source tree..."

distclean: distclean-init rmpatch1 rmpatch2 rmpatch3 rmpatch4
	rm -f patch1 patch2 patch3 patch4
	@becho "Removed patches."
